namespace HtERP.Data
{
    /// <summary>
    /// 数据库连接配置
    /// </summary>
    public class DbConnectionConfig
    {
        /// <summary>
        /// 数据库类型
        /// </summary>
        public string DatabaseType { get; set; } = "SQLite";
        
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string Server { get; set; } = "localhost";
        
        /// <summary>
        /// 端口号
        /// </summary>
        public int Port { get; set; }
        
        /// <summary>
        /// 数据库名称
        /// </summary>
        public string DatabaseName { get; set; } = string.Empty;
        
        /// <summary>
        /// 用户名
        /// </summary>
        public string Username { get; set; } = string.Empty;
        
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; } = string.Empty;
        
        /// <summary>
        /// SQLite数据库文件路径
        /// </summary>
        public string SqliteFilePath { get; set; } = string.Empty;
        
        /// <summary>
        /// Access数据库文件路径
        /// </summary>
        public string AccessFilePath { get; set; } = string.Empty;
        
        /// <summary>
        /// 额外的连接参数
        /// </summary>
        public string AdditionalParams { get; set; } = string.Empty;
        
        /// <summary>
        /// 生成连接字符串
        /// </summary>
        /// <returns>连接字符串</returns>
        public string GenerateConnectionString()
        {
            switch (DatabaseType.ToLower())
            {
                case "sqlserver":
                    return GenerateSqlServerConnectionString();
                case "mysql":
                    return GenerateMySqlConnectionString();
                case "postgresql":
                    return GeneratePostgreSqlConnectionString();
                case "sqlite":
                    return GenerateSqliteConnectionString();
                case "access":
                    return GenerateAccessConnectionString();
                default:
                    return string.Empty;
            }
        }
        
        private string GenerateSqlServerConnectionString()
        {
            var builder = new Microsoft.Data.SqlClient.SqlConnectionStringBuilder
            {
                DataSource = Server + (Port > 0 ? $",{Port}" : ""),
                InitialCatalog = DatabaseName,
                UserID = Username,
                Password = Password,
                IntegratedSecurity = string.IsNullOrEmpty(Username)
            };
            
            if (!string.IsNullOrEmpty(AdditionalParams))
            {
                // 解析额外的连接参数并添加到构建器
                var pairs = AdditionalParams.Split(';', StringSplitOptions.RemoveEmptyEntries);
                foreach (var pair in pairs)
                {
                    var keyValue = pair.Split('=', 2);
                    if (keyValue.Length == 2)
                    {
                        builder.Add(keyValue[0].Trim(), keyValue[1].Trim());
                    }
                }
            }
            
            return builder.ConnectionString;
        }
        
        private string GenerateMySqlConnectionString()
        {
            var builder = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder
            {
                Server = Server,
                Port = (uint)(Port > 0 ? Port : 3306),
                Database = DatabaseName,
                UserID = Username,
                Password = Password
            };
            
            if (!string.IsNullOrEmpty(AdditionalParams))
            {
                // 解析额外的连接参数并添加到构建器
                var pairs = AdditionalParams.Split(';', StringSplitOptions.RemoveEmptyEntries);
                foreach (var pair in pairs)
                {
                    var keyValue = pair.Split('=', 2);
                    if (keyValue.Length == 2)
                    {
                        builder.Add(keyValue[0].Trim(), keyValue[1].Trim());
                    }
                }
            }
            
            return builder.ConnectionString;
        }
        
        private string GeneratePostgreSqlConnectionString()
        {
            var builder = new Npgsql.NpgsqlConnectionStringBuilder
            {
                Host = Server,
                Port = Port > 0 ? Port : 5432,
                Database = DatabaseName,
                Username = Username,
                Password = Password
            };
            
            if (!string.IsNullOrEmpty(AdditionalParams))
            {
                // 解析额外的连接参数并添加到构建器
                var pairs = AdditionalParams.Split(';', StringSplitOptions.RemoveEmptyEntries);
                foreach (var pair in pairs)
                {
                    var keyValue = pair.Split('=', 2);
                    if (keyValue.Length == 2)
                    {
                        builder.Add(keyValue[0].Trim(), keyValue[1].Trim());
                    }
                }
            }
            
            return builder.ConnectionString;
        }
        
        private string GenerateSqliteConnectionString()
        {
            var builder = new System.Data.SQLite.SQLiteConnectionStringBuilder
            {
                DataSource = SqliteFilePath,
                ForeignKeys = true
            };
            
            if (!string.IsNullOrEmpty(AdditionalParams))
            {
                // 解析额外的连接参数并添加到构建器
                var pairs = AdditionalParams.Split(';', StringSplitOptions.RemoveEmptyEntries);
                foreach (var pair in pairs)
                {
                    var keyValue = pair.Split('=', 2);
                    if (keyValue.Length == 2)
                    {
                        builder.Add(keyValue[0].Trim(), keyValue[1].Trim());
                    }
                }
            }
            
            return builder.ConnectionString;
        }
        
        private string GenerateAccessConnectionString()
        {
            var connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AccessFilePath;
            
            if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
            {
                connectionString += $";User ID={Username};Password={Password};";
            }
            
            if (!string.IsNullOrEmpty(AdditionalParams))
            {
                connectionString += $";{AdditionalParams}";
            }
            
            return connectionString;
        }
    }
}